﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.BusinessObjects;
using SCRUM.Core.DAO;
using System.Collections.Specialized;
using SCRUM.Core.Exceptions.Database;   // string dictionary

/// <summary>
/// This widget allows user to manipulate with any Release.
/// User can: filter release, create/edit/delete release.
/// </summary>
public partial class ReleaseWidget : DataOverviewWidgetWithDetails<Release,Project>
{

    #region [ BASE ]

    /// <summary>
    /// Gets the name of the widget. It must be exactly the same as the folder that contains the widget.
    /// </summary>
    /// <value>ReleaseWidget</value>
    public override string Name
    {
        get { return "ReleaseWidget"; }
    }

    /// <summary>
    /// Gets the title. Title is shown in the header of the widget container.
    /// </summary>
    /// <value>The title.</value>
    public override string Title
    {
        get { return (string)GetLocalResourceObject("Title"); }
    }

    #endregion

    #region [ OVERRIDEN ]

    /// <summary>
    /// This method works as a substitute for Page_Load. You should use this method for
    /// data binding etc. instead of Page_Load.
    /// </summary>
    public override void LoadWidget()
    {
        LoadAvailableColumns();
        ApplySettings();
        if (!IsPostBack)
        {
            RefreshWidget();
        }
    }
    /// <summary>
    /// Holds all releases according to specified filter
    /// </summary>
    protected override IList<Release> itemsAcordingToFilter
    {
        get
        {
            int projectID = this.Projects.SelectedProjectID;
            int releaseID = (int)this.FilterID.Value == 0 ? -1 : (int)this.FilterID.Value;
            string name = String.IsNullOrEmpty(this.FilterName.Text) ? null : this.FilterName.Text;
            string version = String.IsNullOrEmpty(this.FilterVersion.Text) ? null : this.FilterVersion.Text;

            return DAORelease.GetAllReleases(projectID, releaseID, -1, name, version );

            //return projectID != -1
            //    ? DAORelease.GetAllReleasesForProject(projectID)
            //    : DAORelease.GetAllReleases();
        }
    }
    #endregion

    #region [ SETTINGS ]

    private const int releaseColumnOffset = 0;

    /// <summary>
    /// Gets a value indicating whether this instance is editable.
    /// </summary>
    /// <value>
    /// 	<c>true</c> if this instance is editable; otherwise, <c>false</c>.
    /// </value>
    public override bool IsEditable
    {
        get { return true; }
    }

    /// <summary>
    /// Shows the edit.
    /// </summary>
    public override void ShowEdit()
    {
        base.ShowEdit();
        this.EditReleaseWidget.Visible = true;
    }

    /// <summary>
    /// Hides the edit.
    /// </summary>
    public override void HideEdit()
    {
        base.HideEdit();
        this.EditReleaseWidget.Visible = false;
    }

    /// <summary>
    /// Occurs when user clicks on Save Settings of PrototypeWidget
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewPageEventArgs"/> instance containing the event data.</param>
    protected void SaveSettingsClick(object sender, EventArgs e)
    {
        SaveSettings();
        this.HideEdit();
        Response.Redirect(Request.Url.ToString());
        //LoadWidget();
    }

    /// <summary>
    /// Refreshes settings section with data from private member settings
    /// </summary>
    private void ShowSettings()
    {
        LabelPageSize.Text = settings["pageSize"];
    }

    /// <summary>
    /// Handles the SelectedPageChanged event of the GridViewData control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewPageEventArgs"/> instance containing the event data.</param>
    public void GridViewData_SelectedPageChanged(object sender, GridViewPageEventArgs e)
    {
        GridViewData.PageIndex = e.NewPageIndex;
        GridViewData.DataBind();
    }

    #endregion

    #region [ ITEM MANIPULATION ]
    protected override void CreateItem()
    {
        ReleaseDetail.CreateItem();
        ShowOperationResult(MessageType.Info, "ReleaseCreated");
    }

    protected override void SaveItem()
    {
        ReleaseDetail.EditItem(selectedItem.ID);
        ShowOperationResult(MessageType.Info, "ReleaseUpdated");
    }

    protected override void DeleteItem(int itemID)
    {
        DAORelease.DeleteRelease(itemID);
        ShowOperationResult(MessageType.Info, "ReleaseDeleted");
    }
    #endregion

    #region [inherited controls mapping]
    // DataOverviewWidget
    protected override TextBox textBoxPageSize { get { return this.TextBoxPageSize; } }
    protected override Panel availableColumnsPanel { get { return this.AvailableColumns; } }
    protected override GridView data { get { return this.GridViewData; } }
    protected override int alwaysVisibleColumn { get { return releaseColumnOffset; } }
    // DataOverviewWidgetWithDetails
    protected override Button buttonCreate { get { return this.ButtonCreate; } }
    protected override Button buttonCreateItem { get { return this.ButtonCreateItem; } }
    protected override Button buttonSaveItem { get { return this.ButtonSaveItem; } }
    protected override Button buttonCancelItem { get { return this.ButtonCancelItem; } }
    protected override Button buttonDeleteItem { get { return this.ButtonDeleteItem; } }
    protected override HierarchicalObjectDetails<Release,Project> itemDetails { get { return this.ReleaseDetail; } }
    protected override int filtersSuperiorItemID { get { return this.Projects.SelectedProjectID; } }
    protected override int filtersProjectID { get { return this.Projects.SelectedProjectID; } }
    #endregion
}
